package com.ibm.epss.android;

import android.util.Log;
import com.ibm.micro.client.mqttv3.*;
import com.ibm.micro.client.mqttv3.internal.MemoryPersistence;

public class MQTTHelper {

    private static MqttClient client;
    private static PkgExpCallBack callback;
    private static ActivityProfile activityProfile = ActivityProfile.getInstance();

    public static boolean isConnected() {
        if (null == client) {
            return false;
        }
        return client.isConnected();
    }

    public static int Connect() {
        try {
            if (null == client) {
                client = new MqttClient(activityProfile.getTCPAddress(),
                        activityProfile.getClientId(), new MemoryPersistence());
            }
            MqttConnectOptions conOptions = new MqttConnectOptions();
            conOptions.setUserName(activityProfile.getUserID());
            conOptions.setPassword(activityProfile.getUserPWD().toCharArray());
            conOptions.setCleanSession(activityProfile.isCleanSession());
            conOptions.setConnectionTimeout(activityProfile.getConnTimeout());
            conOptions.setKeepAliveInterval(3600);
            if (null == callback) {
                callback = new PkgExpCallBack(activityProfile.getClientId(),
                        new PkgExpMsgRevHandler());
            }
            client.setCallback(callback);
            client.connect(conOptions);
        } catch (MqttSecurityException e) {
            Log.e("TonyErrorCode 1", e.getMessage());
            return 1;
        } catch (MqttPersistenceException e) {
            Log.e("TonyErrorCode 2", e.getMessage());
            return 2;
        } catch (MqttException e) {
            Log.e("TonyErrorCode 3", e.getMessage());
            return 3;
        } catch (Exception e) {
            return 4;
        }
        return 0;
    }

    public static int Disconnect() {
        try {
            client.disconnect();
        } catch (Exception e) {
            Log.e("TonyErrorCode", e.getMessage());
            return 1;
        }
        return 0;
    }

    public static int Subscribe() {
        try {
            client.subscribe(activityProfile.getTopicString(), activityProfile.getQoS());
        } catch (Exception e) {
            Log.e("TonyErrorCode", e.getMessage());
            return 1;
        }
        return 0;
    }

    public static int Unsubscribe() {
        try {
            client.unsubscribe(activityProfile.getTopicString());
        } catch (Exception e) {
            Log.e("TonyErrorCode", e.getMessage());
            return 1;
        }
        return 0;
    }

    public static int Publish() {
        try {
            MqttTopic topic = client.getTopic(activityProfile.getTopicString());
            MqttMessage message = new MqttMessage(
                    activityProfile.getPublication().getBytes());
            MqttDeliveryToken token = topic.publish(message);
            // Can use token.waitForCompletion() and token.isComplete() to
            // ensure message is sent
        } catch (Exception e) {
            Log.e("TonyErrorCode", e.getMessage());
            return 1;
        }
        return 0;
    }
}
